<template>
<!-- 具体商品页轮播图 -->
  <div class="banner">
    <div id="slider" class="slider" :style="'margin-left:-'+index+'00%'">
      <span class="iconfont" @click="left()">&#xe779;</span>
      <img v-for="item in imgSrc" :src="item" />
      <span class="iconfont" @click="right()">&#xe775;</span>
    </div>
    <div class="point-box">
      <ol>
        <li
          v-for="(item, i) in imgSrc"
          v-bind:class="i == index ? 'active' : ''"
        ></li>
      </ol>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.banner {
  width: 100%;
  position: relative;
  height: 450px;
  overflow: hidden;
  .slider {
    display: flex;
    width: 100%;
    margin-left: -200%;
    img {
      width: 100%;
    }
    span:nth-of-type(1){
      display: block;
      width: 50px;
      background-color: rgba(194, 194, 194, 0.6);
      font-size: 20px;
      font-weight: bold;
      position: absolute;
      color: rgb(66, 66, 66);
      z-index: 9999;
      text-align: center;
      top: 45%;
      padding: 8% 0;
      left: 0;
    }
    span:nth-of-type(2) {
            display: block;
      width: 50px;
      background-color: rgba(194, 194, 194, 0.6);
      font-size: 20px;
      font-weight: bold;
      position: absolute;
      color: rgb(66, 66, 66);
      z-index: 9999;
      text-align: center;
      top: 45%;
      padding: 8% 0;
      right: 0;
    }
  }
  .point-box {
    position: absolute;
    top: 420px;
    width: 100%;
    ol {
      display: flex;
      margin: 0 auto;
      width: 30%;
      li {
        width: 9px;
        height: 9px;
        margin: 4px;
        border-radius: 50%;
        background-color: #ccc;
        z-index: 9999;
      }
      .active {
        background-color: red;
      }
    }
  }
}
</style>
<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      imgSrc: [],
      startPointX: 0,
      changePointX: 0,
      showIndex: 0,
      index: 0,
    };
  },
  computed: {
    ...mapState(["img"]),
  },
  methods: {
    left() {
      if (this.index == 0) {
        this.index = this.imgSrc.length - 1;
      } else {
        this.index--;
      }
    },
    right() {
      if (this.index == this.imgSrc.length - 1) {
        this.index = 0;
      } else {
        this.index++;
      }
    },
  },
  mounted() {
    let id = this.$route.query.id;
    for (let i = 0; i < this.img.length; i++) {
      if (this.img[i].id == id) {
        this.imgSrc = this.img[i].img;
      }
    }
  },
};
</script>